package org.indi.graduatemanagement.controller;


import cn.hutool.core.lang.Assert;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.indi.graduatemanagement.common.lang.Result;
import org.indi.graduatemanagement.entity.Design;
import org.indi.graduatemanagement.service.DesignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcl
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/design")
public class DesignController {

    @Autowired
    DesignService designService;

    @GetMapping("/{id}")
    @RequiresPermissions("search")
    @RequiresAuthentication //登录后可查询
    public Result getDesignInfoById(@PathVariable("id") String id) {
        Design design = designService.getById(id);
        Assert.notNull(design, "查询错误");
        return Result.successful(design);
    }

    @PostMapping("/save")
    @RequiresPermissions("add")
    @RequiresAuthentication //登录后可查询
    public Result saveDesignInfo(@Validated @RequestBody Design design) {
        if (designService.getById(design.getGraId()) != null) {
            return Result.fail("信息已存在");
        }
        designService.save(design);
        return Result.successful(null);
    }

    @PostMapping("/update")
    @RequiresPermissions("update")
    @RequiresAuthentication //登录后可查询
    public Result updateDesignInfo(@Validated @RequestBody Design design) {

        if (designService.getById(design.getGraId()) == null) {
            return Result.fail("信息不存在");
        }
        designService.updateById(design);
        return Result.successful(null);
    }

    @DeleteMapping("/{id}")
    @RequiresPermissions("delete")
    @RequiresAuthentication //登录后可查询
    public Result deleteDesignInfoById(@PathVariable("id") String id) {
        if (designService.removeById(id))
            return Result.successful(null);
        else
            return Result.fail("删除失败");
    }

}
